<template>
  <div class="underlay">
    <div class="img-info">
      <img :src="require(`@/assets/png/online-map/${mapUrl}.png`)" alt="" />
    </div>
    <div class="label-text" @click="showImg">替换样式</div>
  </div>
  <div class="underlay-img" v-if="visible">
    <div class="top-title">
      <span>选择底图</span>
      <img src="@/assets/png/close.png" alt="" @click="visible = false" />
    </div>
    <div class="img-list">
      <div class="img-item" v-for="(item, i) in mapList" :key="i" @click="chooseCurrent(item)">
        <div class="img-item-box">
          <img :src="require(`@/assets/png/online-map/${item.extra.icon}.png`)" alt="" />
        </div>
        <span>{{ item.name }}</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useStageStore } from '@/store/stage';
import { mapList } from '@/enum/map';

const stageStore = useStageStore();
const visible = ref(false);

const showImg = () => {
  visible.value = true;
};

const mapUrl = computed(() => {
  return stageStore.layerData.data.icon || 'jl';
});

const chooseCurrent = (item: any) => {
  if (item.type) {
    stageStore.layers.forEach((info) => {
      if (
        info.type === 'IMAGE' &&
        (info?.data?.source === 'tianditu' ||
          info?.data?.source === 'fileserver' ||
          info?.data?.source === 'ditu' ||
          info?.data?.source === 'qditu')
      ) {
        stageStore.removeLayer(info.id);
      }
    });
    const layer = {
      title: item.name,
      ...item,
      data: {
        icon: item.extra.icon,
        mapId: item.mapId,
      },
    };
    stageStore.addStageLayer(layer);
    visible.value = false;
  }
};

watch(
  () => stageStore.rightVisible,
  (value) => {
    if (!value && visible.value) visible.value = false;
  }
);
</script>

<style lang="less" scoped>
.underlay {
  display: flex;
  align-items: center;
  position: relative;
  .label-text {
    width: 68px;
    height: 24px;
    background: #0560fd;
    border-radius: 2px;
    text-align: center;
    line-height: 24px;
    color: #fff;
    font-size: 12px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    margin-left: 11px;
    cursor: pointer;
    user-select: none;
  }
  .img-info {
    width: 68px;
    height: 68px;
    background: #303030;
    border-radius: 3px;
    border: 1px solid #464646;
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 46px;
      height: 46px;
    }
  }
}
.underlay-img {
  width: 428px;
  box-sizing: border-box;
  background: #2e2e2e;
  border-radius: 4px;
  position: fixed;
  top: 180px;
  right: 300px;
  z-index: 1000;
  .top-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    color: #ffffff;
    padding: 5px 13px 10px;
  }
  .img-list {
    display: flex;
    flex-wrap: wrap;
    cursor: pointer;
    .img-item {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column;
      margin-bottom: 10px;
      width: 25%;
      font-size: 10px;
      font-family: PingFangSC, PingFang SC;
      color: #ffffff;
      .img-item-box {
        width: 80px;
        height: 80px;
        background: #232322;
        border-radius: 4px;
        border: 1px solid #0560fd;
        display: flex;
        align-items: center;
        justify-content: center;
        img {
          width: 60px;
          height: 60px;
        }
      }
    }
  }
}
</style>
